package com.neuedu.web.service;

import com.neuedu.web.dao.UmsUserDao;
import com.neuedu.web.po.UmsUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;

/**
 * @author 金山
 * 项目：javaweb
 * site: https://blog.fulfill.com.cn
 * 描述    跟登录有关的业务逻辑
 * @data 2024/11/1815:39
 */
public class LoginService {

    UmsUserDao userDao = new UmsUserDao();




    public boolean login(HttpServletRequest request , HttpServletResponse response) throws IOException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {



        String userName = request.getParameter("userName");
        String password = request.getParameter("password");


        String sql = " select * from ums_user where username = ? and password = ?    ";
        UmsUser user = userDao.selectOne(sql, userName, password);

        if (user != null   ) {

            // if(user.getExpire().before(new Date())){
            //
            // }


            HttpSession session = request.getSession();

            // System.out.println(session.getMaxInactiveInterval());
            session.setAttribute("userName", userName);
            session.setAttribute("user", user);


            //更新最后一次登录时间
            user.setLastlogin(new Date());
            userDao.update(user);
            // JSONUtils.write(response, Res.ok(true));

            return true;

        } else {
            System.out.println("登陆失败");
            // JSONUtils.write(response, Res.ok(false));

            return false;

        }


    }



}
